﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using System.Drawing;
using OpenTK.Graphics;
using Common;

namespace T10_Transparency {
    class T10_TransparencyGameWindow : GameWindow {
        public T10_TransparencyGameWindow() {
            this.Load += T10_TransparencyGameWindow_Load;
            this.Resize += T10_TransparencyGameWindow_Resize;
            this.RenderFrame += T10_TransparencyGameWindow_RenderFrame;
            this.Unload += T10_TransparencyGameWindow_Unload;
            this.Width = 1280;
            this.Height = 720;
            this.Title = "T10_TransparencyGameWindow";
            FpsCounter.FrameMillsecondTick += FpsCounter_FrameMillsecondTick;
        }

        private void FpsCounter_FrameMillsecondTick(double obj) {
            Console.WriteLine($"{obj}ms/frame");
        }

        private void T10_TransparencyGameWindow_RenderFrame(object sender, FrameEventArgs e) {
            GL.ClearColor(0, 0, .4f, 0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            Render();

            SwapBuffers();
            ProcessEvents();
        }
        ModelMesh mesh;
        Shader shader;
        Texture2D texture;
        Vbo<Vector3> vertexBuffer, normalBuffer;
        Vbo<Vector2> uvBuffer;
        Vbo<ushort> indexBuffer;
        VertexAttribManager attribs = new VertexAttribManager();
        private void T10_TransparencyGameWindow_Load(object sender, EventArgs e) {

            VSync = VSyncMode.Off;
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);
            GL.Enable(EnableCap.CullFace);
            shader = new Shader("shader.vs", "shader.fs");

            mesh = MeshLoader.LoadObjIndexed("suzanne.obj");
            texture = Texture2D.Load("uvmap.dds");
            vertexBuffer = new Vbo<Vector3>(mesh.Vertices);
            uvBuffer = new Vbo<Vector2>(mesh.Uvs);
            normalBuffer = new Vbo<Vector3>(mesh.Normals);
            indexBuffer = new Vbo<ushort>(BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticDraw, mesh.Indices);

            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

            attribs.AddAttrib(3, vertexBuffer);
            attribs.AddAttrib(2, uvBuffer);
            attribs.AddAttrib(3, normalBuffer);



        }

        private void Render() {
            shader.Use();
            InputControls.computeMatricesFromInput(this);
            FpsCounter.CountFps();
            var model = Matrix4.Identity;
            var mvp = model * InputControls.ViewMatrix * InputControls.ProjectionMatrix;
            shader.SetMatrix4("M", ref model);
            shader.SetMatrix4("V", ref InputControls.ViewMatrix);
            shader.SetMatrix4("MVP", ref mvp);

            Vector3 lightPos = new Vector3(4, 4, 4);
            shader.SetVector3("LightPosition_worldspace", lightPos);
            texture.Active(TextureUnit.Texture1, shader, "myTextureSampler");
            attribs.Enable();
            GL.DrawElements(BeginMode.Triangles, mesh.Indices.Length, DrawElementsType.UnsignedShort, 0);
            attribs.Disable();

        }

        private void T10_TransparencyGameWindow_Resize(object sender, EventArgs e) {
            GL.Viewport(0, 0, Width, Height);
        }



        private void T10_TransparencyGameWindow_Unload(object sender, EventArgs e) {
            GlDisposableObjectBase.DisposeObjects(shader, texture,vertexBuffer,uvBuffer,normalBuffer,indexBuffer,mesh);
        }

    }
}
